import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import com.View.TreeModel 1.0
import QtQuick.Dialogs 1.3

ApplicationWindow {
    id: root
    visible: true
    width: 940
    height: 680
    title: qsTr("雁联风控管理端")
    objectName: "rootWnd"
    property string colon: ":"
    property TreeModel menuTreeModel
    property var selectColor: "#FF3399FF"

    Menu {
        // 右键菜单
        id: rBtnMenu
        property var row
        property var col
        property var model
        MenuItem {
            text: qsTr("复制单元格数据")
            onTriggered: {
                rBtnMenu.model.copyCellText(rBtnMenu.row, rBtnMenu.col)
            }
        }
    }

    Component {
        id: toolBtnStyle
        Rectangle {
            color: "red"
        }
    }

    Action {
        id: pwdAc
        text: "密码"
        shortcut: "ctrl+x"
        iconSource: "../image/SearchPic.png"
        iconName: "edit-cut"
        onTriggered: console.debug("密码")
    }

    Action {
        id: lockAc
        text: "锁定"
        shortcut: "ctrl+c"
        iconSource: "../image/editcopy.png"
        iconName: "edit-cut"
        onTriggered: {
            lockWnd.show()
            root.hide()
        }
    }

    Action {
        id: logoutAc
        text: "注销"
        //        tooltip: "logout"
        shortcut: "ctrl+v"
        iconSource: "../image/fileopen.png"
        iconName: "edit-cut3"
        onTriggered: logoutMsgbox.open()
    }
    toolBar: ToolBar {
        width: parent.width
        height: 50
        RowLayout {
            anchors.fill: parent
            spacing: 0
            ToolButton {
                action: logoutAc
                Layout.preferredWidth: 40
                Layout.preferredHeight: 40
            }
            ToolButton {
                action: lockAc
            }
            ToolButton {
                action: pwdAc
                Layout.preferredWidth: parent.height
                Layout.preferredHeight: parent.height
            }

            Item {
                Layout.fillWidth: true
            }
        }
    }

    SplitView {
        anchors.fill: parent
        orientation: Qt.Horizontal

        TreeView {
            id: menuTree
            //            Layout.preferredWidth: 60
            width: 150
            Layout.minimumWidth: 100

            headerVisible: false
            backgroundVisible: false
            model: menuTreeModel //g_MainQuo.getTreeMenu()

            TableViewColumn {
                title: "Name"
                role: "nodeText"
                resizable: true
            }

            onDoubleClicked: {
                if (isExpanded(currentIndex))
                    collapse(currentIndex)
                else
                    expand(currentIndex)
            }

            onClicked: {
                if (menuTreeModel.isLeafNode(currentIndex)) {
                    console.debug("click Leaf")
                    g_MainQuo.openMenu(currentIndex)
                } else
                    console.debug("click not Leaf")
            }

            Component.onCompleted: {
                menuTreeModel = g_MainQuo.getTreeMenu()
                //展开树
                var nRow = menuTreeModel.rowCount(menuTree.rootIndex)
                console.debug("Tree rowCount " + nRow)
                for (var i = 0; i < nRow; ++i) {
                    var leve1Index = menuTreeModel.index(i, 0,
                                                         menuTree.rootIndex)
                    menuTree.expand(leve1Index) //1级节点一般都是非叶子节点，直接展开
                    //展开2级节点，本次不需要
                    //                    var nLevel1RowCount = menuTreeModel.rowCount(leve1Index)
                    //                    console.debug("Tree1 rowCount " + nLevel1RowCount)
                    //                    for (var k = 0; k < nLevel1RowCount; ++k) {
                    //                        var leve2Index = menuTreeModel.index(k, 0, leve1Index)
                    //                        if (!menuTreeModel.isLeafNode(leve2Index))
                    //                            menuTree.expand(leve2Index)
                    //                    }
                }
            }
        }

        TabView {
            id: frame
            Layout.fillWidth: true
        }
    }

    statusBar: StatusBar {
        id: statusBar
        RowLayout {
            anchors.fill: parent

            Rectangle {
                //                color: "lightgray"
                //                Layout.preferredHeight: parent.height
                anchors.left: parent.left
                anchors.top: parent.top
                anchors.bottom: parent.bottom
                Layout.preferredWidth: 160
                Text {
                    anchors.centerIn: parent
                    //                    text: g_MainQuo.getStateText(0)
                    //                    color: "white"
                }
            }
            Rectangle {
                //                color: "black"
                //                Layout.preferredHeight: parent.height
                anchors.top: parent.top
                anchors.bottom: parent.bottom
                anchors.right: parent.right
                Layout.preferredWidth: 160
                Layout.preferredHeight: 24 //需要有这样1对width-height，不然输出错误信息不爽
                //                Layout.fillWidth: true
                Text {
                    id: timeText
                    anchors.centerIn: parent
                    //                    text: currentDTime()
                    //                    color: "white"
                }

                Timer {
                    id: timeRun
                    interval: 1000
                    repeat: true
                    onTriggered: {
                        //                        timeText.text = currentDTime();
                        timeText.text = g_MainQuo.getTradeTime()
                    }
                }

                Component.onCompleted: {
                    timeRun.start()
                }
            }
        }
    }

    MsgDlg {
        id: logoutMsgbox
        standardButtons: StandardButton.Yes | StandardButton.No
        modality: Qt.ApplicationModal
        title: "提示"
        text: "你确定要注销吗？"
        onYes: {
            g_MainQuo.logout()
            console.debug("logoutt ")
            root.close()
        }
    }
    MsgDlg {
        id: tipInput //输入非法的提示
        standardButtons: StandardButton.Close
        title: "提示"
        visible: false
        //        text: "请输入正确的查询人数"
    }
    LockWnd {
        id: lockWnd
    }
    ColumnSet_Edit {
        id: colSet
    }

    ListModel {
        id: acctTypeModel
        ListElement {
            text: " "
            acct_type: 0
        }
        ListElement {
            text: "法人"
            acct_type: 2
        }
        ListElement {
            text: "个人"
            acct_type: 3
        }
    }
    ListModel {
        id: acctTypeModel2

        ListElement {
            text: "法人"
            acct_type: 2
        }
        ListElement {
            text: "个人"
            acct_type: 3
        }
    }

    function isTabExist(tabName) {
        console.log(" frame.count " + frame.count)
        for (var k = frame.count - 1; k >= 0; k--) {
            var tabText = frame.getTab(k).title
            console.log(tabText + "is visible tab " + tabName)
            if (tabName === tabText) {
                return k
            }
        }

        return -1
    }

    function openTabPage(tabName, fileName) {
        var nExist = isTabExist(tabName)
        if (nExist >= 0)
            frame.currentIndex = nExist
        else {
            var compoment = Qt.createComponent(fileName)
            var tabPage = frame.addTab(tabName, compoment)
            tabPage.active = true
            tabPage.item.tabName = tabName
            console.log("addTabs 1" + tabName)
            if (!tabPage) {
                console.log(qsTr('加载tab失败'))
                return
            }

            frame.currentIndex = frame.count - 1
        }
    }

    function openRiskWatch(tabName) {
        var nExist = isTabExist(tabName)
        if (nExist >= 0)
            //            frame.activeFocusOnTab(nExist)
            frame.currentIndex = nExist
        else {
            var compoment = Qt.createComponent("RiskWatch.qml")
            var tabPage = frame.addTab(tabName, compoment)
            tabPage.active = true
            tabPage.item.tabName = tabName
            console.log("addTabs 1" + tabName)
            if (!tabPage) {
                console.log(qsTr('加载tab失败'))
                return
            }

            //            if (compoment.status == Component.Ready) {
            //                tabPage.item.branchStat = g_MainQuo.getBranchStat()
            //            }
            frame.currentIndex = frame.count - 1
        }
    }

    function openGradeSet(tabName) {
        var nExist = isTabExist(tabName)
        if (nExist >= 0)
            frame.currentIndex = nExist
        else {
            var compoment = Qt.createComponent("GradeSet.qml")

            //        var tabName = "风险监控"
            var tabPage = frame.addTab(tabName, compoment)
            tabPage.active = true
            tabPage.item.tabName = tabName
            console.log("addTabs " + tabName)
            if (!tabPage) {
                console.log(qsTr('加载tab失败'))
                return
            }

            //            if (compoment.status == Component.Ready) {
            //                tabPage.item.branchStat = g_MainQuo.getBranchStat()
            //            }
            frame.currentIndex = frame.count - 1
        }
    }

    function openRiskCustList(tabName, branchID) {
        var nExist = isTabExist(tabName)
        if (nExist >= 0) {
            frame.currentIndex = nExist
            frame.getTab(nExist).item.branchID = branchID
        } else {
            var compoment = Qt.createComponent("RiskCustList.qml")
            var tabPage = frame.addTab(tabName, compoment)
            tabPage.active = true
            tabPage.item.tabName = tabName
            tabPage.item.branchID = branchID
            console.log("addTabs " + tabName)
            if (!tabPage) {
                console.log(qsTr('加载tab失败'))
                return
            }

            //            if (compoment.status == Component.Ready) {
            //                tabPage.item.branchStat = g_MainQuo.getBranchStat()
            //            }
            frame.currentIndex = frame.count - 1
        }
    }
    function openSingleForce(tabName, acctNo2) {
        var nExist = isTabExist(tabName)
        if (nExist >= 0) {
            frame.currentIndex = nExist
            frame.getTab(nExist).item.acctNo = acctNo2
        } else {
            var compoment = Qt.createComponent("SingleForce.qml")

            var tabPage = frame.addTab(tabName, compoment)
            tabPage.active = true
            tabPage.item.tabName = tabName
            tabPage.item.acctNo = acctNo2
            console.log("addTabs " + tabName)
            if (!tabPage) {
                console.log(qsTr('加载tab失败'))
                return
            }

            //            if (compoment.status == Component.Ready) {
            //                tabPage.item.branchStat = g_MainQuo.getBranchStat()
            //            }
            frame.currentIndex = frame.count - 1
        }
    }

    function openBatchForce(tabName, branchID) {
        var nExist = isTabExist(tabName)
        if (nExist >= 0) {
            frame.currentIndex = nExist
            frame.getTab(nExist).item.branchID = branchID
        } else {
            var compoment = Qt.createComponent("BatchForce.qml")
            var tabPage = frame.addTab(tabName, compoment)
            tabPage.active = true
            tabPage.item.tabName = tabName
            tabPage.item.branchID = branchID
            console.log(branchID + "addTabs " + tabName)
            if (!tabPage) {
                console.log(qsTr('加载tab失败'))
                return
            }

            frame.currentIndex = frame.count - 1
        }
    }

    function closeTab(tabName) {
        var nExist = isTabExist(tabName)
        if (nExist >= 0)
            frame.removeTab(nExist)
    }

    function currentDTime() {
        // "yyyy-MM-dd hh:mm:ss.zzz ddd"
        return Qt.formatDateTime(new Date(), "hh:mm:ss")
    }
}
